<html>
<head>
 
</head>
<body style="text-align=center;font-size:32px;">
<table align="center">
<tr><td><div id="problemNum" style='background-color:#999999;width:800;text-align:center;font-size:32px;'></div></td></tr>
<tr><td><div id="problemContent" style='word-wrap:break-word;background-color:#bbbbbb;width:800;text-align:left;font-size:20px;'></div></td></tr>
<tr><td><div id="sum" style='word-wrap:break-word; color:#ffff22;font-size:48;background-color:#8855ff;width:800;text-align:center;'></div></td></tr>
<tr><td><div id="copyleft" style='word-wrap:break-word; color:#ffff22;font-size:18;background-color:#666666;width:800;text-align:right;'></div></td></tr>
<script language="javascript">
    //---------------------------------//
    // Project Euler 
    //
    // Author:thrombin
    //   Date:2015-12-13
    //---------------------------------//  
var p_order=14;//Problem Order
 
var problem='The following iterative sequence is defined for the set of positive integers:<br/>\
n --> n/2 (n is even)<br/>\
n --> 3n + 1 (n is odd)<br/>\
Using the rule above and starting with 13, we generate the following sequence:<br/>\
13 --> 40 --> 20 --> 10 --> 5 --> 16 --> 8 --> 4 --> 2 --> 1<br/>\
It can be seen that this sequence (starting at 13 and finishing at 1) contains 10 terms. <br/>\
Although it has not been proved yet (Collatz Problem), it is thought that all starting numbers finish at 1.<br/>\
Which starting number, under one million, produces the longest chain?<br/>\
NOTE: Once the chain starts the terms are allowed to go above one million.';
 
 
 
//solve the problem
//==============编程思路简介================
//  暴力穷举，注意以下几点
//      1、chain_n + 1 = chaiin_2n
//=====================================
var M=1e6;
var i,n;
var max_len=0,max_n;
var collatz=0;
 
for(n=parseInt(M/2);n<M;n++){
    collatz=1;
    for(i=n;i>1;collatz++){
        if(i%2==0){i=parseInt(i/2);}
        else{i=parseInt(3*i+1);}        
    }
    if(collatz>max_len){
        max_len=collatz;
        max_n=n;
    }
}
 
 
//update browser
document.getElementById("problemNum").innerHTML="Project Euler-Problem "+p_order;
document.getElementById("problemContent").innerHTML=problem;
document.getElementById("sum").innerHTML="Answer:"+max_n+"("+max_len+")";
document.getElementById("copyleft").innerHTML="CopyLeft@Thrombin    2015";
</script>
</body>
</html>